package com.wyh.rabbitextensionsdemo.config;

import com.wyh.rabbitextensionsdemo.constant.Constants;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.transaction.RabbitTransactionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TranConfig {
    @Bean("tranQueue")
    public Queue tranQueue(){
        return QueueBuilder.durable(Constants.TRAN_QUEUE).build();
    }

    @Bean("tranExchange")
    public DirectExchange tranExchange(){
        return ExchangeBuilder.directExchange(Constants.TRAN_EXCHANGE).durable(true).build();
    }

    @Bean("tranBinding")
    public Binding tranBinding(){
        return BindingBuilder.bind(tranQueue()).to(tranExchange()).with(Constants.TRAN_ROUTING_KEY);
    }

    @Bean("tranRabbitTemplate")
    public RabbitTemplate tranRabbitTemplate(ConnectionFactory connectionFactory){
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setChannelTransacted(true);
        return rabbitTemplate;
    }

    @Bean("tranRabbitTransactionManager")
    public RabbitTransactionManager tranRabbitTransactionManager(ConnectionFactory connectionFactory){
        return new RabbitTransactionManager(connectionFactory);
    }
}
